package com.jiepos.mpos.biz.appInfM.controller;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.jiepos.mpos.biz.affix.service.AppAffixInfService;
import com.jiepos.mpos.biz.appInfM.service.HistoryService;
import com.jiepos.mpos.biz.user.service.AppInsInfService;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Affix_InfEntity;
import com.jiepos.mpos.common.entity.App_Ins_AppEntity;
import com.jiepos.mpos.common.entity.App_Ins_App_HistoryEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;


/**
 *
 *
 * <p>标题: —— 系统参数控制器</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @author 余翼膑
 * @version 1.0
 * @since 2017年9月25日
 * @history（历次修订内容、修订人、修订时间等）
 */
@Controller
@RequestMapping("history")
public class HistoryController extends BaseController {
    @Autowired
    private HistoryService historyService ;

    @Autowired
    private AppAffixInfService appAffixInfService;

    @Autowired
    private AppInsInfService appInsInfService;

    /**
     * 进入APP历史更新信息列表页面
     * @param request
     * @param response
     * @param pagination
     * @param
     * @return
     */
    @RequestMapping("/getHistoryAppList.do")
    public ModelAndView getHistoryAppList(HttpServletRequest request, HttpServletResponse response,
                                       Pagination pagination) {
        ModelAndView model = new ModelAndView("/module/appInfM/history/historyApp_list");
        return super.getDetail(request, response, model);
    }
    /**
     * 查询参数列表信息json数据
     * @param
     * @param
     * @param pagination
     * @param appInsAppEntity
     * @return
     */
    @RequestMapping("/getHistoryAppListJson.do")
    public @ResponseBody
    Map getHistoryAppListJson(HttpServletRequest request,Pagination pagination, App_Ins_AppEntity appInsAppEntity) {

        return historyService.getHistoryAppList(request,pagination, appInsAppEntity);
    }
    /**
     * 进入APP信息详细页面
     * @param request
     * @param response
     * @param pagination
     * @param ins_app_history_uuid
     * @return
     */
    @RequestMapping("/getHistoryAppDetail.do")
    public ModelAndView getHistoryAppDetail(HttpServletRequest request, HttpServletResponse response,
                                              Pagination pagination,String ins_app_history_uuid) {
        ModelAndView model = new ModelAndView("/module/appInfM/history/historyApp_detail");
        // 菜单编码
        Map infEntity = historyService.getHistoryAppById(ins_app_history_uuid);
        model.addObject("infEntity", infEntity);
        return super.getDetail(request, response, model);
    }
    /**
     * 保存机构
     * @param request
     * @param response
     * @param appHistoryEntity
     * @return
     */
    @RequestMapping(value="/saveHistoryAppDetail.do", method= RequestMethod.POST)
    public ModelAndView saveHistoryAppDetail(HttpServletRequest request, HttpServletResponse response,App_Ins_App_HistoryEntity appHistoryEntity,
                                             @RequestParam(value = "apkFile",required = false) MultipartFile apkFile) {
        String ins_app_history_uuid = historyService.saveHistoryAppDetail(appHistoryEntity, super.getLoginUser(request, response));
        //上传文件操作
        if(apkFile != null && !apkFile.isEmpty()){
            //apk文件不为空 那么进行保存操作
            //根据机构app uuid 得到机构号
            String ins_cd = appInsInfService.getInsCDByInsAppUUID(appHistoryEntity.getIns_app_uuid());
            //上传文件
            boolean result = appAffixInfService.uploadApkFile(request,apkFile,ins_app_history_uuid,appHistoryEntity.getIns_app_uuid(),ins_cd,
                    appHistoryEntity.getApp_version(),super.getLoginUser(request, response));
        }
        Map map=super.getLoginUser(request, response);
        return new ModelAndView(new RedirectView("getHistoryAppList.do"), map);
    }
    /**
     * 删除APP历史更新
     * @param request
     * @param response
     * @param
     * @return
     */
    @RequestMapping("/deleteHistoryAppByIds.do")
    public ModelAndView deleteHistoryAppByIds(HttpServletRequest request, HttpServletResponse response, String ins_app_history_uuids) {
        historyService.deleteHistoryAppByIds(ins_app_history_uuids);
        Map map = super.getLoginUser(request, response);
        return new ModelAndView(new RedirectView("getHistoryAppList.do"), map);
    }
    /**
     * 获取机构菜单树
     * @return
     */
    @RequestMapping("/getInsAppInfTree.do")
    @ResponseBody
    public List<Map<String, Object>> getInsAppInfTree(){

        return historyService.getInsAppInfTree();
    }
    /**
     * 获取apk下载地址
     * @return
     */
    @RequestMapping("/getDownloadUrl.do")
    @ResponseBody
    public String getDownloadUrl(HttpServletRequest request, String ins_app_history_uuid) throws Exception{
        String baseUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
        App_Affix_InfEntity entity = appAffixInfService.getAffixInfByParam(ins_app_history_uuid, SystemConstant.AFFIX_TP_05,SystemConstant.STORE_TP_05_01);
        if(entity!=null){
            return baseUrl + entity.getAffix_path();
        }else{
            return "null";
        }
    }

}
